import query from './atom/db/query';
import count from './atom/db/count';
import { IBaseTable } from './atom/db/interfaces';

declare const action_id: string;
declare const session_id: string;
declare const spaceid: string;

interface Message {
	filter: {};
	sort: {};
	page_size: number;
	page_no: number;
}

interface CheckAttendanceRule extends IBaseTable {
    staff_name: string; staff_number: string; deptid: string; deptname: string; f_name: number; blood_type: number; zodiac: string; native_place: string; birthday: number; age: string; memo: string; sex: string; id_no: string; marital_status: string; politics_face: string; entry_party_time: number;
    up_time: number | string;down_time:number | string;
}


function format(date: number | string) {
	const _date = new Date(parseFloat(date + ''));
	let res = '';
	if (_date) {
		res = _date.getFullYear() + '-' + _format(_date.getMonth() + 1) + '-' + _format(_date.getDate());
	}
	return res;
}

function _format(d: number) {
	if (d < 10) {
		return '0' + d;
	} else {
		return d;
	}
}

async function atom(msg: Message) {
	const systemid = '6051';
	const { exec, prepare } = query(action_id, session_id, spaceid, systemid);
	const asc: string[] = [];
	const desc: string[] = [];
	if (msg.sort) {
		const key = Object.keys(msg.sort)[0];
		if (msg.sort[key] === -1) {
			desc.push(key);
		} else {
			asc.push(key);
		}
	}
	// 考勤规则设置
	prepare('check_attendance_rule', ['_id', 'rule_no', 'rule_name', 'start_month', 'end_month', 'up_time', 'down_time', 'is_valid', 'memo'], msg.filter, msg.page_size, msg.page_no, asc, desc);
	const [data] = await exec();

	data.forEach((item: CheckAttendanceRule ) => {
        item['up_time'] = item.up_time ? format(item.up_time) : '';
        item['down_time'] = item.down_time ? format(item.down_time ) : '';
		
	});

	const c = count(action_id, session_id, spaceid, systemid);
	c.prepare('check_attendance_rule', msg.filter);
	const [t] = await c.exec();

	return { dataArray: data, count: t };
}